﻿
var ns, toast, actionsheet, UIActionProgress;
var app = new Vue({
    el: '#app',
    data: {
        userinfo: {
            headimg: '../image/default.png',
            username: '',
            phonenumber: '',
            loginname: '',
            sex:'',
            positionname:'',
        },
        appVersion:"",
        appUpdata:0//1最新版 2有跟新
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                action: 'getuserinfo',
            }, function (succeed, data, err) {
                console.log('版本号：'+JSON.stringify(api.appVersion));
                that.appVersion = api.appVersion;//app版本号
                if(data.status){
                    that.userinfo.headimg = data.headimg || '../image/default.png';
                    that.userinfo.username = data.username;
                    that.userinfo.phonenumber = data.phonenumber;
                    that.userinfo.loginname = data.loginname;
                    that.userinfo.sex = data.sex;
                    that.userinfo.positionname = data.positionname;
                };
            });
            that.getAppVersionIsUpdate();
        },
        getAppVersionIsUpdate: function(){
            let that = this;
            ns.post({
                action: 'getappversion',
                version: api.appVersion,
                versiontype: api.systemType,
                APPType: 2
            }, function(succeed, data, err) {
                if(data && data.update) that.appUpdata=2;
                else  that.appUpdata=1;
            }, {
                is_getapi: true
            });
        },
        change_nickname: function() {
            ns.openWin('xiugainicheng_frm', '修改昵称');
        },
        change_phone: function() {
            ns.openWin('xiugaidianhua_frm', '修改手机号码');
        },
        openActionsheet: function() {
            let that = this;
            ns.confirmPer('camera', function() {
                that.openActionsheet_Process();
            })
        },
        openActionsheet_Process: function() {
            var that = this;
            actionsheet.init({
                frameBounces: true, //当前页面是否弹动，（主要针对安卓端）
                cancelTitle: '关闭',
                buttons: ['拍照', '图库选择']
            }, function(ret) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        that.take_picture();
                    } else if (ret.buttonIndex == 2) {
                        that.choose_picture();
                    }
                }
            })
        },
        take_picture: function() {
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                mediaValue: 'pic',
                destinationType: 'url',
                allowEdit: true,
                quality: 80,
                saveToPhotoAlbum: false
            }, function(ret, err) {
                if (ret && ret.data) {
                    that.userinfo.headimg = ret.data;
                    that.do_save_headimg();
                }
            });
        },
        choose_picture: function() {
            var that = this;
            api.getPicture({
                sourceType: 'album',
                encodingType: 'jpg',
                mediaValue: 'pic',
                destinationType: 'url',
                allowEdit: true,
                quality: 80,
            }, function(ret, err) {
                if (ret && ret.data) {
                    that.userinfo.headimg = ret.data;
                    that.do_save_headimg();
                }
            });
        },
        do_save_headimg: function() {
            var that = this;
            var filelist = [];
            filelist.push(that.userinfo.headimg);
            toast.loading({
                title: "提交中",
                duration: 2000
            }, function(ret) {});
            ns.post({
                action: 'saveheadimg'
            }, function(succeed, data, err) {
                toast.hide();
                that.is_searching = false;
                if (succeed) {
                    if (data.headimg) {
                        api.setPrefs({
                            key: 'headimg',
                            value: data.headimg
                        });
                        api.sendEvent({
                            name: 'changecentersuccess'
                        });
                        api.toast({
                            msg: '头像上传成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: filelist
                }
            });
        },
        image_cache: function(url) {
            var that = this;
            api.imageCache({
                url: url
            }, function(ret, err) {
                if (ret.status) {
                    that.userinfo.headimg = ret.url;
                }
            });
        },
        // 修改密码
        changepwd: function() {
            ns.openWin('xiugaimima_frm', '修改密码');
        },
        // 版本更新
        checkupdate: function() {
            appupgrade.check_upgrade(true);
        },
        // 退出登录
        logout: function() {
            api.confirm({
                title: '提示',
                msg: '确定退出当前账号',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    ns.removeUid(true);
                };
            });
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    UIActionProgress = api.require('UIActionProgress');
    app.get_data();
    api.addEventListener({
        name: 'changecentersuccess'
    }, function() {
        app.get_data();
    });
}
